//
//  BuKeGuanLiVC.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/24.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BuKeGuanLiVC.h"
#import "CJClassSelectedView.h"
#import "CJYearMonthSelectedView.h"
#import "TeacherClassListModel.h"

@interface BuKeGuanLiVC ()<BuKeGuanLiNavViewDelegate,BKXianXiaShengQingViewDelegate,GoFaFangZiYuanViewDelegate>
{
    CGFloat mainw;
    int currentPage;
}
@property (nonatomic, assign) BOOL Type;



@property (nonatomic, strong) UILabel *titleTopLab;//标题




/**
 关键字
 */
@property (nonatomic, copy) NSString *keyWord;
@property (nonatomic, strong) SYNoLabel *nolab;

/**
 选中班级的model
 */
@property (nonatomic, strong) TeacherClassListModel *banJiSelectedModel;

@end

@implementation BuKeGuanLiVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = MAINBACKCOLOR;
    _banji = @"0";
    _dataTime = @"";
    _keyWord = @"";
    _nolab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self.view];
    [self.leftView.tableList headerRefreshBlock:^{
        [self requestData];
    }];
//    [self.leftView.tableList fooderRefreshBlock:^{
//
//    }];
    [self.rightView.tableList headerRefreshBlock:^{
         [self xianXiaShengQIng];
    }];
//    [self.rightView.tableList fooderRefreshBlock:^{
//
//    }];
    [self requestData];
    [self xianXiaShengQIng];
}


- (void)requestData {
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestLessonTutorLessonLineApplyIsOnLine:@"1" classId:_banji date:_dataTime keyWord:_keyWord pageIndex:0 pageSize:1000 success:^(NSMutableArray *listArr) {
       // NSLog(@"线上申请 %@",listArr);
        [MBProgressHUD mj_hideHUDForView:self.view];
        [weakSelf.leftView requestDataArr:listArr];
        [weakSelf.leftView.tableList.mj_header endRefreshing];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self.view];
        [weakSelf.leftView.tableList.mj_header endRefreshing];
    }];
}
#pragma mark - 添加线下申请
- (void)xianXiaShengQIng{
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestLessonTutorLessonLineApplyIsOffLine:@"0" classId:@"0" date:_dataTime keyWord:_keyWord pageIndex:0 pageSize:1000 success:^(NSMutableArray *listArr) {
        // NSLog(@"线下申请 %@",listArr);
        [MBProgressHUD mj_hideHUDForView:self.view];
        [weakSelf.rightView requestDataArr:listArr];
        [weakSelf.rightView.tableList.mj_header endRefreshing];
    } fail:^(NSError *error) {
       // NSLog(@"线下申请失败 %@",error);
        [MBProgressHUD mj_hideHUDForView:self.view];
        [weakSelf.rightView.tableList.mj_header endRefreshing];
    }];
}
//http://apipad.31waiyu.com//tutor/LessonTutorService.svc/TutorLessonOnLineApply
- (void)initSubView{
    currentPage = 0;
    mainw  = MAINW;
    _Type = YES;
    [self.view addSubview:self.navView];
    _navView.mainw = mainw;
    [_navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    
    [self.navView addSubview:self.titleTopLab];
    [self.titleTopLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.navView.mas_centerX);
        make.centerY.equalTo(self.navView.mas_centerY);
    }];
    
    
    [self addNotice];
    [self.view addSubview:self.mainScrollView];
    [_mainScrollView addSubview:self.leftView.view];
    [_mainScrollView addSubview:self.rightView.view];
    
}
- (void)layout{
    
}
-(void)addNotice{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchTextField:) name:@"keyString" object:nil];
}

- (void)searchTextField:(NSNotification *)notification {
    NSString *str = notification.userInfo[@"keyString"];
    _keyWord = str;
    [self requestData];
}

- (BuKeGuanLiNavView *)navView{
    if (!_navView) {
        BuKeGuanLiNavView *view = [[BuKeGuanLiNavView alloc]initWithFrame:CGRectMake(0, 0, MAINW-150, 44) andArray:@[]];
        view.backgroundColor = MAINBACKCOLOR;
        view.delegate = self;
        _navView = view;
    }
    return _navView;
}
- (BKXianShangShengQingView *)leftView{
    if (!_leftView) {
        BKXianShangShengQingView *view = [[BKXianShangShengQingView alloc]init];
        view.view.frame = CGRectMake(0, 0, mainw, APP_HEIGHT-84);
        view.Type = self.Type;
        view.mainDelegate = self;
        _leftView = view;
    }
    return _leftView;
}
- (BKXianXiaShengQingView *)rightView{
    if (!_rightView) {
        BKXianXiaShengQingView *view = [[BKXianXiaShengQingView alloc]init];
        view.view.frame = CGRectMake(mainw, 0, mainw, APP_HEIGHT-84);
        view.Type = self.Type;
        view.manDelegate = self;
        _rightView = view;
    }
    return _rightView;
}
- (void)BKXianXiaShengQingViewAddDianMing:(DataLessonModel *)model{
    //NSLog(@"前往点名 %@",model.tutorLessonId);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _dianMingView = [[BKXXDianMingView alloc]initWithFrame:CGRectMake(mainw, 0, mainw+40, self.view.frame.size.height)];
    [_dianMingView getShangkeDianmingRequest];
    [self.view addSubview:self.dianMingView];
    [_dianMingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
//    [UIView animateWithDuration:0.5 animations:^{
//        self->_dianMingView.frame = CGRectMake(0, 0, self->mainw+40, self.view.frame.size.height);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
//    }];
    
}
- (void)BKXianXiaShengQingViewAddCheck:(DataLessonModel *)model{
   // NSLog(@"前往详情 %@",model.tutorLessonId);
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _chaKanXueShengView = [[BKXXSQCheckStudentsView alloc]initWithFrame:CGRectMake(mainw, 0, mainw+40, self.view.frame.size.height) andFuDaoID:model.tutorLessonId];
    [self.view addSubview:self.chaKanXueShengView];
    [_chaKanXueShengView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
//    [UIView animateWithDuration:0.5 animations:^{
//        self->_chaKanXueShengView.frame = CGRectMake(0, 0, self->mainw+40, self.view.frame.size.height);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
//    }];
}
//前往选择资源
- (void)goFaFangZiYuanView:(NSMutableArray *)dataArr{
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    _faFangZiYuanView = [[BKGLFaFangZiYuanView alloc]initWithFrame:CGRectMake(mainw, 0, mainw+20, self.view.frame.size.height)];
    [self.view addSubview:self.faFangZiYuanView];
    [_faFangZiYuanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.bottom.equalTo(@0);
    }];
//    [UIView animateWithDuration:0.5 animations:^{
//        self->_faFangZiYuanView.frame = CGRectMake(0, 0, self->mainw+40, self.view.frame.size.height);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
//    }];
    [_faFangZiYuanView requestDataListIdArr:dataArr];
}
- (UIScrollView *)mainScrollView{
    if (!_mainScrollView) {
        UIScrollView *view = [[UIScrollView alloc]init];
        view.frame = CGRectMake(0, 44, MAINW, APP_HEIGHT-84);
        view.contentSize = CGSizeMake(MAINW*2, 0);
        view.showsHorizontalScrollIndicator = YES;
        view.showsVerticalScrollIndicator = NO;
        view.pagingEnabled = YES;
        view.scrollEnabled = NO;
        view.backgroundColor = MAINBACKCOLOR;
        _mainScrollView = view;
    }
    return _mainScrollView;
}
- (void)scrollViewChangeButtom:(int)index{
    currentPage = index;
    self.mainScrollView.contentOffset = CGPointMake(mainw*index, 0);
    if (index == 0) {
        
    }else{
        if (!_rightView) {
            [_mainScrollView addSubview:self.rightView.view];
        }
    }
}
- (void)nianJiSelected{
    [CJClassSelectedView showDatePickerWithTitle:@"选择班级" minDateStr:@"年级一" resultBlock:^(TeacherClassListModel * _Nonnull selectValue) {
      //  NSLog(@"输出选择的班级 %@",selectValue);
        self->_banJiSelectedModel = selectValue;
        self->_navView. banJiButton.bottomLabel.text = selectValue.name;
        self.banji = selectValue.id;
        [self requestData];
    }];
}
- (void)riQiSelected{
    [CJYearMonthSelectedView showDatePickerWithTitle:@"选择月份" minDateStr:@"2000-01" resultBlock:^(NSString *selectValue) {
        if ([selectValue isEqualToString:@"全部"]) {
            self->_navView.timeButton.bottomLabel.text = @"全部";
        } else {
            //选择完成后的操作
            NSArray *array = [selectValue componentsSeparatedByString:@"-"];
            self->_navView.timeButton.bottomLabel.text = [NSString stringWithFormat:@"%@年%@月",array[0],array[1]];
        }
        
        
    }];
 
}

- (void)changeView:(NSNotification *)info{
    BOOL type = [[info.userInfo objectForKey:@"Type"] intValue];
    self.Type = !type;
    mainw = [PublickFunctionObject currentScreenSize:type];
    //NSLog(@"大小 %f",mainw);
    _navView.mainw = mainw;
    [_navView uploadLayout];
    
    _mainScrollView.frame = CGRectMake(0, 0, mainw, APP_HEIGHT-84);
    _mainScrollView.contentOffset = CGPointMake(currentPage*mainw, 0);
    _leftView.view.frame = CGRectMake(0, 0, mainw, APP_HEIGHT-84);
    _leftView.Type = self.Type;
    [_leftView.tableList reloadData];
    
    _rightView.view.frame = CGRectMake(mainw, 0, mainw, APP_HEIGHT-84);
    _rightView.Type = self.Type;
    [_rightView.tableList reloadData];
    
    _dianMingView.frame = CGRectMake(0, 0, mainw+40,APP_HEIGHT);
    [_dianMingView.tableList reloadData];
    
    _chaKanXueShengView.frame = CGRectMake(0, 0, mainw+40, APP_HEIGHT);
    [_chaKanXueShengView.tableList reloadData];
    
    _faFangZiYuanView.frame = CGRectMake(0, 0, mainw+40, APP_HEIGHT);
    [_faFangZiYuanView.tableView reloadData];
    _faFangZiYuanView.selectedResourceHeader.frame = CGRectMake(0, 44, mainw+40, APP_HEIGHT);
    [_faFangZiYuanView.selectedResourceHeader.tableList reloadData];
    
}

- (UILabel *)titleTopLab {
    if (!_titleTopLab) {
        
        _titleTopLab = [UILabel titleColour:TITLECOLOR
                              titleFount:16
                                pingfang:@"PingFang-SC-Regular"
                               alignment:NSTextAlignmentCenter
                               titleLine:1];
        _titleTopLab.text = @"线上申请";
    }
    return _titleTopLab;
}

@end
